#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
class CCat
{
    private:
        string name;
        int heavy;
    public:
        void set(string _na,int _he)
        {
            name=_na;
            heavy=_he;
        }
        string getname()
        {
            return name;
        }
        friend bool cmp(const CCat& a,const CCat& b);
};
bool cmp(const CCat& a,const CCat& b)
{
    return a.heavy<b.heavy;
}
int main()
{
    int num;
    cin>>num;
    CCat* cats=new CCat[num];
    for(int i=0;i<num;i++)
    {
        string _na;
        int _he;
        cin>>_na>>_he;
        cats[i].set(_na,_he);
    }
    sort(cats,cats+num,cmp);
    for(int i=0;i<num;i++)
    {
        cout<<cats[i].getname()<<" ";
    }
    cout<<endl;
}